<?php
/**
 * CanAccess
 * Determine if the user is allowed to go to the specified module/controller/action
 *
 * @package     Visi
 * @subpackage  View Helpers
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2011 Coalition Technologies <joel@coalitiontechnologies.com>
 * @version     $Id$
 */
class Visi_View_Helper_CanAccess extends Zend_View_Helper_Abstract {

    /**
     * Determine if the user has access to a specific resource
     *
     * @param array An array of module/controller/action to determine if we have access to
     */
    public function canAccess(Array $params) {
    
        $front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();
        $resource = array();
        $module = array_key_exists('module', $params) ? $params['module'] : null;
        if (!array_key_exists('controller', $params) || !array_key_exists('action', $params)) {
            throw new InvalidArgumentException('CanAccess view helper requires an array with controller and action as keys.');
        }
        if ($params['controller'] == 'error') {
            $resourceName = 'error';
        } elseif ($module && $module != $front->getDefaultModule()) {
            $resource[] = $module;
            $resource[] = $params['controller'];
            $resourceName = implode(':', $resource);
        } else {
            // @todo This is a hack, need to figure out why acl is using :controller
            // instead of simply "controller"
            $resourceName = ':' . $params['controller'];
        }
        // Check if the controller/action can be accessed by the current user
        $allowed = false;
        // a list of roles to check for access
        $aclPlugin = $front->getPlugin('Zion_Controller_Plugin_Acl');
        $role = $aclPlugin->getRoleName();
        $acl = $aclPlugin->getAcl();
        return $acl->isAllowed($role, $resourceName, $params['action']);
    
    }

}
